﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Comod_ComoditisManage : ATPages
{
    ZSql zs = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            yjTm1.Attributes.Add("onclick", "SelectDate('yjTm1')");
            yjTm2.Attributes.Add("onclick", "SelectDate('yjTm2')");
            HFroid.Value = Request.Cookies["user"].Values["roleid"];
            initGvData();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            //this.GvData.Columns[0].Visible = false;
            // btnAdd.Enabled = false;
        }
    }

    private void initGvData()
    {
         string sql = "";
         if (HFroid.Value == "22")
         {
             sql = "select ComoditiesId,comodicode,a.WarehouseId,b.WarehouseName,c.Supplier,d.ComoditiesType,e.Brand,f.ComoditiesName,i.Specifications,j.Units,convert(char(10),storageTime,120) as storDate ,a.comodid,ComoditiesBarcode,Prices1,Prices2,SeveralStorage,TotalMoney,a.EditUserName,a.AuditState from CS_Comoditiles a join CS_Warehouse b on a.WarehouseId=b.WarehouseId join CS_Supplier c on a.SupplierId=c.SupplierId join ComoditiesType d on a.ComoditiesType=d.TypeId join Brand e on a.brand=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.Specifications=i.SpecificationsId join Units j on a.Units=j.UnitsId where a.WarehouseId='24' order by b.warehousename";
         }
         else
         {
             sql = "select ComoditiesId,comodicode,a.WarehouseId,b.WarehouseName,c.Supplier,d.ComoditiesType,e.Brand,f.ComoditiesName,i.Specifications,j.Units,convert(char(10),storageTime,120) as storDate ,a.comodid,ComoditiesBarcode,Prices1,Prices2,SeveralStorage,TotalMoney,a.EditUserName,a.AuditState from CS_Comoditiles a join CS_Warehouse b on a.WarehouseId=b.WarehouseId join CS_Supplier c on a.SupplierId=c.SupplierId join ComoditiesType d on a.ComoditiesType=d.TypeId join Brand e on a.brand=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.Specifications=i.SpecificationsId join Units j on a.Units=j.UnitsId order by b.warehousename";
         }
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;

    }

    protected void linkbtnUodate_Click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument.Split('|')[0];
        string state = (sender as LinkButton).CommandArgument.Split('|')[1];
        if (state == "未审核")
        {
            ClientScript.RegisterStartupScript(typeof(Page), "dd", "alert('审核后才可修改！');", true);
        }
        else
        {
            Response.Redirect("Comodities.aspx?id=" + id + "&action=update&page=2&moduid=" + Request["moduid"]);
        }

    }
    protected void lnkbtnFlag_Click(object sender, EventArgs e)
    {
        string id = (sender as LinkButton).CommandArgument;
        Response.Redirect("Comodities.aspx?id=" + id + "&action=flag&page=2&moduid=" + Request["moduid"]);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (this.GvData.Rows.Count > 0)
        {
            for (int i = 0; i < GvData.Rows.Count; i++)
            {
                CheckBox ck = GvData.Rows[i].Cells[10].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {

                    zs.Open("delete CS_Comoditiles  where ComodiCode='" + this.GvData.Rows[i].Cells[0].Text + "'");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (yjTm1.Text == "" && yjTm2.Text != "")
        {
            Response.Write("<script>alert('日期不能为空');location.href=document.referrer</script>");
        }
        if (yjTm2.Text == "" && yjTm1.Text != "")
        {
            Response.Write("<script>alert('日期不能为空');location.href=document.referrer</script>");

        }
        string sql = "";
        if (HFroid.Value == "22")
        {
            sql = "select ComoditiesId,comodicode,a.WarehouseId,b.WarehouseName,c.Supplier,d.ComoditiesType,e.Brand,f.ComoditiesName,i.Specifications,j.Units,convert(char(10),storageTime,120) as storDate ,a.comodid,ComoditiesBarcode,Prices1,Prices2,SeveralStorage,TotalMoney,a.EditUserName,a.AuditState from CS_Comoditiles a join CS_Warehouse b on a.WarehouseId=b.WarehouseId join CS_Supplier c on a.SupplierId=c.SupplierId join ComoditiesType d on a.ComoditiesType=d.TypeId join Brand e on a.brand=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.Specifications=i.SpecificationsId join Units j on a.Units=j.UnitsId where a.WarehouseId='24' order by b.warehousename";
        }
        else
        {
            sql = "select ComoditiesId,comodicode,a.WarehouseId,b.WarehouseName,c.Supplier,d.ComoditiesType,e.Brand,f.ComoditiesName,i.Specifications,j.Units,convert(char(10),storageTime,120) as storDate ,a.comodid,ComoditiesBarcode,Prices1,Prices2,SeveralStorage,TotalMoney,a.EditUserName,a.AuditState from CS_Comoditiles a join CS_Warehouse b on a.WarehouseId=b.WarehouseId join CS_Supplier c on a.SupplierId=c.SupplierId join ComoditiesType d on a.ComoditiesType=d.TypeId join Brand e on a.brand=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.Specifications=i.SpecificationsId join Units j on a.Units=j.UnitsId ";
        }
        if (!string.IsNullOrEmpty(yjTm1.Text) && !string.IsNullOrEmpty(yjTm2.Text.Trim()))
        {
            sql += " and storageTime>='" + yjTm1.Text.Trim() + " 00:00:00' and storageTime<='" + yjTm2.Text.Trim() + " 23:59:59'";
        }
        if (!string.IsNullOrEmpty(txttype.Text))
        {
            sql += " and d.ComoditiesType like'%" + txttype.Text + "%'";
        }
        if (!string.IsNullOrEmpty(txtck.Text))
        {
            sql += " and b.WarehouseName like'%" + txtck.Text + "%'";
        }
        if (!string.IsNullOrEmpty(txtbh.Text))
        {
            sql += " and i.modulbh like'%" + txtbh.Text + "%'";

        }
        if (!string.IsNullOrEmpty(txtSpe.Text))
        {
            sql += " and i.Specifications like'%" + txtSpe.Text + "%'";

        }
        if (ddlzt.SelectedValue!="0")
        {
            sql += " and a.AuditState ='"+ddlzt.SelectedValue+"'";

        }

        //string sql="select ComoditiesId,comodicode,b.WarehouseName,c.Supplier,d.ComoditiesType,e.Brand,f.ComoditiesName,i.Specifications,j.Units,convert(char(10),storageTime,120) as storDate ,comodid,ComoditiesBarcode,Prices1,Prices2,SeveralStorage,TotalMoney,a.EditUserName,a.AuditState from CS_Comoditiles a join CS_Warehouse b on a.WarehouseId=b.WarehouseId join CS_Supplier c on a.SupplierId=c.SupplierId join ComoditiesType d on a.ComoditiesType=d.TypeId join Brand e on a.brand=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.Specifications=i.SpecificationsId join Units j on a.Units=j.UnitsId where a.Specifications ='" + zs.m_table.Rows[0]["SpecificationsId"].ToString() + "'";
        //PageNavigator1.Total = 1;
        //PageNavigator1.Query = sql;
        sql += " order by b.warehousename";

        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;

    }


    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], Request["moduid"], 10, GvData, btnDel);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ZSql cz = new ZSql();
            ZSql update = new ZSql();
            string AuditState = DataBinder.Eval(e.Row.DataItem, "AuditState").ToString();
            string ComoditiesId = DataBinder.Eval(e.Row.DataItem, "ComoditiesId").ToString();
            string WarehouseId = DataBinder.Eval(e.Row.DataItem, "WarehouseId").ToString();
            cz.Open("select * from CS_Stock where WarehouseId='" + WarehouseId + "' and ComoditiesId='" + ComoditiesId + "'");
            if (cz.m_table.Rows.Count > 0)
            {
                //(e.Row.Cells[9].FindControl("lnkbtnEdit") as LinkButton).Enabled = false;
                (e.Row.Cells[9].FindControl("linkbtnFlag") as LinkButton).Enabled = false;
                (e.Row.Cells[10].FindControl("ckFlag") as CheckBox).Enabled = false;
            }
            else 
            {
                update.Open("update CS_Comoditiles set AuditState='未审核' where ComoditiesId=" +Convert.ToInt32(ComoditiesId) + "");
            }
        }
    }
}
